const defaults = {
    title: undefined,
    parent: null,
    message: '',
    type: '',
    showClose: true,
    modalFade: true,
    lockScroll: true,
    closeOnClickModal: true,
    closeOnPressEscape: true,
    showConfirmButton: true,
    showCancelButton: false,
    confirmButtonType: 'primary',
    cancelButtonType: '',
    confirmButtonDisabled: false,
    cancelButtonDisabled: false,
    confirmButtonPosition: 'right',
    confirmButtonHighlight: false,
    cancelButtonHighlight: false,
    confirmButtonLoading: false,
    cancelButtonLoading: false,
    confirmButtonText: '',
    cancelButtonText: '',
    confirmButtonClass: '',
    cancelButtonClass: '',
    beforeClose: null
};

import Vue from 'vue';
import GMessageBox from './g-message-box';
import { merge } from '@/utils/assist';

const MessageBoxConstructor = Vue.extend( GMessageBox );

const MessageBox = function ( options, callback ) {
    if ( typeof options === 'string' ) {
        options = {
            message: options
        };
        if ( arguments[ 1 ] ) {
            options.title = arguments[ 1 ];
        }
        if ( arguments[ 2 ] ) {
            options.type = arguments[ 2 ];
        }
    } else if ( options.callback && !callback ) {
        callback = options.callback;
    }

    return new Promise( ( resolve, reject ) => {

        let instance = new MessageBoxConstructor( {
            el: document.createElement( 'div' )
        } );

        for ( let prop in options ) {
            if ( options.hasOwnProperty( prop ) ) {
                instance[ prop ] = options[ prop ];
            }
        }
        if ( options.callback === undefined ) {

            instance.callback = (action) => {
                if ( typeof callback === 'function' ) {
                    if (instance.showInput) {
                        callback(instance.inputValue, action);
                    } else {
                        callback(action);
                    }
                }
   
                let $type = options.$type;
                if ( $type === 'confirm' || $type === 'prompt' ) {
                    if ( action === 'confirm' ) {
                        resolve( {value: instance.inputValue, action} );
                    } else if ( action === 'cancel') {
                        reject( action );
                    }
                } else {
                    resolve( action );
                }

                setTimeout( () => {
                    if ( instance.parent && instance.parent instanceof HTMLElement ) {
                        instance.parent.removeChild( instance.$el );
                    } else {
                        document.body.removeChild( instance.$el );
                    }

                    instance.$destroy();
                }, 2000 );

            }
        }

        [ 'modal', 'showClose', 'closeOnClickModal', 'closeOnPressEscape' ].forEach( prop => {
            if ( instance[ prop ] === undefined ) {
                instance[ prop ] = true;
            }
        } );

        if ( instance.parent && instance.parent instanceof HTMLElement ) {
            instance.modalAppendToBody = false;
            instance.parent.appendChild( instance.$el );
        } else {
            instance.modalAppendToBody = false;
            document.body.appendChild( instance.$el );
        }

        Vue.nextTick( () => {
            instance.visible = true;
        } );

    } );
};

MessageBox.setDefaults = defaults => {
    MessageBox.defaults = defaults;
};

MessageBox.alert = ( message, title, options ) => {
    if ( typeof title === 'object' ) {
        options = title;
        title = '';
    }
    return MessageBox( merge( {
        title: title,
        message: message,
        $type: 'alert',
        closeOnPressEscape: false,
        closeOnClickModal: false
    }, options ) );
};

MessageBox.prompt = (message, title, options) => {
    if (typeof title === 'object') {
        options = title;
        title = '';
    }
    return MessageBox(merge({
        title: title,
        message: message,
        showCancelButton: true,
        showInput: true,
        $type: 'prompt'
    }, options));
};

MessageBox.confirm = ( message, title, options ) => {
    if ( typeof title === 'object' ) {
        options = title;
        title = '';
    }
    return MessageBox( merge( {
        title: title,
        message: message,
        $type: 'confirm',
        showCancelButton: true
    }, options ) );
};

export default MessageBox;
export { MessageBox };
